package controllers;

import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Vector;

import models.CustomTable;
import models.CustomTableModel;
import models.TopSellingItemsModel;
import system.View;
import dialogs.TopSellingItemsDialog;

public class TopSellingItemsController extends AbstractController
{
	TopSellingItemsModel model1 = new TopSellingItemsModel();
	public TopSellingItemsController(View v) {
		super(v);
		// TODO Auto-generated constructor stub
	}

	public int validateEntries(String date1, String n)
	{
		DateFormat df = new SimpleDateFormat("dd/MM/yy");
		java.util.Date d = null;
		try 
		{
			d = df.parse(date1);
		} 
		catch (ParseException e) 
		{
			e.printStackTrace();
		}
		java.sql.Date sqltDate = new java.sql.Date(d.getTime());
		
		ResultSet rs = model1.getTopSellingItems(Integer.valueOf(n));
		if(rs!=null)
		{
			ResultSet rs1 = model1.show();
			CustomTableModel model = new CustomTableModel(model1.getConnection(), rs);
			CustomTable data = new CustomTable(model);
	
			model.addExceptionListener(this);
			data.addExceptionListener(this);
		    
			view.addTable(data);
			return operationSuccess();
		}
		return operationFailure();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		String actionCommand = e.getActionCommand();
		if (actionCommand.equals("Top Selling Items"))
		{
			showDialogWindow(new TopSellingItemsDialog(view, this));
		}		
	}

	@Override
	public int validateInsert(Vector<String> s) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int validateDelete(Vector<String> s) {
		// TODO Auto-generated method stub
		return 0;
	}

}
